#include "consignee_main.h"
#include "ui_consignee_main.h"

Consignee_main::Consignee_main(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Consignee_main)
{
    ui->setupUi(this);
    this->setFixedSize(440,720);

    socket = SocketManager::getinstance().socket();

    flush_data();
}

Consignee_main::~Consignee_main()
{
    delete ui;
}

void Consignee_main::flush_data()
{
    sendRig meg;
    ui->listWidget->clear();
    telephone_number = Telephone_Number::teleinstance().telenum();
    meg.type=4;
    strcpy(meg.tele,telephone_number.toUtf8());
    meg.IC=1;
    QByteArray data;
    data.append((char*)&meg,sizeof (sendRig));
    socket->write(data);
    connect(socket,&QAbstractSocket::readyRead,this,&Consignee_main::f_data);
}

void Consignee_main::f_data()
{
    if(!this->isActiveWindow()){
        return;
    }
    sendRig  data;
    while (socket->bytesAvailable() >= sizeof(sendRig)) {
            socket->read(reinterpret_cast<char*>(&data), sizeof(sendRig));
    }
//    data = (sendRig*)socket->readAll().data();

    QListWidgetItem * item;
    item = new QListWidgetItem;
    item->setText(QString("%1  单号:%2  %3 身份码：%4")
                  .arg(data.Account)
                  .arg(data.ID)
                  .arg(data.state)
                  .arg(data.password));
    ui->listWidget->addItem(item);
}

void Consignee_main::on_pushButton_clicked()
{
    flush_data();
}
